<template>
  <el-dialog
    title="分配权限"
    :visible.sync="opSetPermissOn"
    @close="cancelEvent"
  >
    <el-tree
      ref="tree"
      check-strictly
      show-checkbox
      :data="list"
      :props="{ label: 'name' }"
      node-key="id"
      :default-checked-keys="defaultKeys"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="cancelEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermissionAPI, sysRoleGETAPI, sysroleAssignPremAPI } from '@/api'
export default {
  data() {
    return {
      opSetPermissOn: false,
      list: [{}],
      defaultKeys: [], // 选中的值,
      id: ''
    }
  },
  created() {
    this.getPer()
  },
  methods: {
    async getPer() {
      const res = await sysPermissionAPI()

      this.list = this.changeData(res.data, '0')
    },
    changeData(arr, pid) {
      return arr.filter((v) => {
        if (v.pid == pid) {
          v.children = this.changeData(arr, v.id)
          return true
        }
      })
    },
    // 获取用户详情
    async getDateId(id) {
      const res = await sysRoleGETAPI(id)

      this.id = id
      // this.defaultKeys = res.data.permIds
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    cancelEvent() {
      this.opSetPermissOn = false
      this.$refs.tree.setCheckedKeys([])
    },
    async submit() {
      const res = await sysroleAssignPremAPI(
        this.id,
        this.$refs.tree.getCheckedKeys()
      )
      this.cancelEvent()
      this.$message.success('修改成功')
    }
  }
}
</script>

<style></style>
